package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.purchaser.entity.BusTicketPoolEntity;
import com.taxshare.purchaser.mapper.BusTicketPoolMapper;
import com.taxshare.purchaser.view.dto.BusTicketDTO;
import com.taxshare.response.beans.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author YANG
 * @date 2021/4/20
 * @Description:汽车票发票池
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class BusTicketPoolService extends ServiceImpl<BusTicketPoolMapper, BusTicketPoolEntity> {

    private static String timeStart = " 00:00:00";
    private static String timeEnd = " 23:59:59";

    /**
     * 汽车票发票池查询
     *
     * @param query
     * @return
     */
    public Response busPoolQuery(PageRequest<BusTicketDTO> query) {
        BusTicketDTO busTicketDTO = query.getInfo();
        String createTimeStart = busTicketDTO.getCreateTimeStrStart();
        String createTimeEnd = busTicketDTO.getCreateTimeStrEnd();
        QueryWrapper<BusTicketPoolEntity> busPoolQuery = new QueryWrapper<>();
        if (StrUtil.isNotBlank(busTicketDTO.getInvoiceNo())) {
            busPoolQuery.eq("invoice_no", busTicketDTO.getInvoiceNo());
        }
        if (StrUtil.isNotBlank(createTimeStart) && StrUtil.isNotBlank(createTimeEnd)) {
            busPoolQuery.between("create_time_str", createTimeStart + timeStart, createTimeEnd + timeEnd);
        }
        busPoolQuery.orderByDesc("create_time");
        IPage<BusTicketPoolEntity> busTicketPoolEntityIPage = PageHelp.siamplePage(this, busPoolQuery, query.getPage(), BusTicketPoolEntity.class);
        return Response.success(busTicketPoolEntityIPage);
    }
}
